-- SELECT REGISTRO 40

				Select	Filial					=	A.Filial,
						Cliente					=	A.Nome_CliFor,
						Codigo_Filial			=	A.Cod_Filial,
						Serie					=	A.Serie_NF,
						Numero_NF				=	A.Numero_NF,
						Codigo_Fiscal_Operacao	=	Ltrim(Rtrim(MIN(A.Codigo_Fiscal_Operacao))),
						Tipo_Registro			=	'40|',
						Base_ICMS				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Base_ICMS, 0)), 15, 2))), '.', ','))) + '|',
						Valor_ICMS				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_ICMS, 0)), 15, 2))), '.', ','))) + '|',
						Base_ICMS_ST			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Base_ICMS_ST, 0)), 15, 2))), '.', ','))) + '|',
						Valor_ICMS_ST			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_ICMS_ST, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Produto			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Bruto, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Frete				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Frete, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Seguro			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Seguro, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Desconto			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Desconto, 0)), 15, 2))), '.', ','))) + '|',
						Valor_IPI				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_IPI, 0)) + SUM(ISNULL(A.VALOR_IMPOSTO_AGREGAR_ITEM, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Outras_Despesas	=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Outras_Despesas, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Total				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Liquido, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Total_Servico		=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Servicos, 0)), 15, 2))), '.', ','))) + '|',
						Aliquota_ISS			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Aliquota_ISS, 0)), 15, 2))), '.', ','))) + '|',
						Valor_ISS				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_ISS, 0)), 15, 2))), '.', ','))) + '|'
				From	Dbo.FX_LF_Item_Entrada('E1', '001026', 'E', '20090701', '20090731') As A
				Where	A.Nota_Cancelada			=	0																									And
						A.Numero_Modelo_Fiscal		In	('01', Case When IsNumeric(A.Numero_Modelo_Fiscal)	=	1	Then	'01'	Else	'1A'	End)	And
						A.Codigo_Fiscal_Operacao	<	'8000' AND NF_ENTRADA_PROPRIA = 1
				Group By	A.Filial, A.Nome_CliFor, A.Cod_Filial, A.Serie_NF, A.Numero_NF

				Union All

				Select	Filial					=	A.Filial,
						Cliente					=	A.Nome_CliFor,
						Codigo_Filial			=	A.Cod_Filial,
						Serie					=	A.Serie_NF,
						Numero_NF				=	A.Numero_NF,
						Codigo_Fiscal_Operacao	=	Ltrim(Rtrim(MIN(A.Codigo_Fiscal_Operacao))),
						Tipo_Registro			=	'40|',
						Base_ICMS				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Base_ICMS, 0)), 15, 2))), '.', ','))) + '|',
						Valor_ICMS				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_ICMS, 0)), 15, 2))), '.', ','))) + '|',
						Base_ICMS_ST			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Base_ICMS_ST, 0)), 15, 2))), '.', ','))) + '|',
						Valor_ICMS_ST			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_ICMS_ST, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Produto			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Bruto, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Frete				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Frete, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Seguro			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Seguro, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Desconto			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Desconto, 0)), 15, 2))), '.', ','))) + '|',
						Valor_IPI				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_IPI, 0)) + SUM(ISNULL(A.VALOR_IMPOSTO_AGREGAR_ITEM, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Outras_Despesas	=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Outras_Despesas, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Total				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Liquido, 0)), 15, 2))), '.', ','))) + '|',
						Valor_Total_Servico		=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_Servicos, 0)), 15, 2))), '.', ','))) + '|',
						Aliquota_ISS			=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Aliquota_ISS, 0)), 15, 2))), '.', ','))) + '|',
						Valor_ISS				=	Ltrim(Rtrim(Replace(Ltrim(Rtrim(Str(Sum(IsNull(A.Valor_ISS, 0)), 15, 2))), '.', ','))) + '|'
				From	Dbo.FX_LF_Item_Saida('E1', '001026', '20100801', '20100830') As A
				Where	A.Nota_Cancelada			=	0																									And
						A.Numero_Modelo_Fiscal		In	('01', Case When IsNumeric(A.Numero_Modelo_Fiscal)	=	1	Then	'01'	Else	'1A'	End)	And
						A.Codigo_Fiscal_Operacao	<	'8000'
				Group By	A.Filial, A.Nome_CliFor, A.Cod_Filial, A.Serie_NF, A.Numero_NF
				Order By	Numero_NF, Serie, Filial



